package com.srwl.mytx.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.srwl.mytx.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fan.feng on 2018/10/17.
 */

public class TimeSelectorView extends View {
    private static final String TAG = "TimeSelectorView";
    private static final int NONE = -3;
    private static final int AM = -2;
    private static final int PM = -1;

    private Paint mPaint;
    private Paint mTextPaint;

    //选中颜色
    private int selectedBgColor;
    //未选中颜色
    private int unSelectedBgColor;
    //边框颜色
    private int borderColor;
    //边框宽度
    private float borderWidth;

    //文本颜色
    private int selectedTextColor;
    private int unSelectedTextColor;
    private float textSize;

    private int width;
    private int height;

    private int row = 2;
    private int rol = 13;

    private boolean isSelect = false;

    private List<Integer> selectedList;
    private float childerWidth;
    private float childerHeight;
    private Map<Integer, Object> maxPriceRecord = new HashMap<>();

    //最后触摸位置
    private int lastTouchPosition = NONE;

    public TimeSelectorView(Context context) {
        this(context, null);
    }

    public TimeSelectorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeSelectorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TimeSelectorView);
        //获取自定义属性和默认值

        //文本相关
        selectedTextColor = mTypedArray.getColor(R.styleable.TimeSelectorView_TSTextSelectedColor, 0xFFFFFFFF);
        unSelectedTextColor = mTypedArray.getColor(R.styleable.TimeSelectorView_TSTextUnSelectedColor, 0xFF333333);
        textSize = mTypedArray.getDimension(R.styleable.TimeSelectorView_TSTextSize, 16);

        //背景相关
        selectedBgColor = mTypedArray.getColor(R.styleable.TimeSelectorView_TSSelectedBackgroundColor, 0xFF3AA6E8);
        unSelectedBgColor = mTypedArray.getColor(R.styleable.TimeSelectorView_TSUnSelectedBackgroundColor, 0xFFECECEC);

        //边框相关
        borderColor = mTypedArray.getColor(R.styleable.TimeSelectorView_TSBorderColor, 0xFFDDDDDD);
        borderWidth = mTypedArray.getDimension(R.styleable.TimeSelectorView_TSBorderWidth, 1);

        mTypedArray.recycle();

        mPaint = new Paint();
        mTextPaint = new Paint();


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

        width = getMeasuredWidth();
        height = getMeasuredHeight();

        childerWidth = (float) width / rol;
        childerHeight = (float) height / row;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);

        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < rol; j++) {

                int drawPosition = NONE;

                if (j == 0) {

                    if (i == 0) {
                        drawPosition = AM;
                    } else {
                        drawPosition = PM;
                    }

                } else {
                    if (i == 0) {
                        drawPosition = j - 1;
                    } else {
                        drawPosition = j + 11;
                    }
                }

                if (drawPosition >= 0) {

                    boolean isContains = false;

                    if (selectedList != null && selectedList.contains(new Integer(drawPosition))) {
                        isContains = true;
                    }

                    //如果当前色块选中的
                    if (isContains) {
                        //设置未选中背景
                        mPaint.setColor(selectedBgColor);
                        mTextPaint.setColor(selectedTextColor);
                    } else {
                        //设置未选中背景
                        mPaint.setColor(unSelectedBgColor);
                        mTextPaint.setColor(unSelectedTextColor);
                    }
                } else {
                    //设置未选中背景
                    mPaint.setColor(unSelectedBgColor);
                    mTextPaint.setColor(unSelectedTextColor);
                }

                RectF rectF = new RectF(j * childerWidth, i * childerHeight, j * childerWidth + childerWidth, i * childerHeight + childerHeight);

                //画背景块
                canvas.drawRect(rectF, mPaint);

                //画文字
                mTextPaint.setTextSize(textSize);

                //计算baseline
                Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
                float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
                String drawText = getTimeForNum(drawPosition);
                if (drawPosition != AM && drawPosition != PM) {
                    float baseline = rectF.centerY() - 3 * distance;
                    canvas.drawText(drawText, rectF.centerX() - mTextPaint.measureText(drawText) / 2, baseline, mTextPaint);
                    baseline = rectF.centerY() + 4 * distance;
                    String price = "0.1";
                    if (maxPriceRecord.containsKey(drawPosition)) {
                        Object o = maxPriceRecord.get(drawPosition);
                        price = o.toString();
                    }
                    //画价格
                    canvas.drawText(price, rectF.centerX() - mTextPaint.measureText(price) / 2, baseline, mTextPaint);

                    //画分割线
                    canvas.drawRect(rectF.left + 15, rectF.centerY() - borderWidth / 2, rectF.right - 15, rectF.centerY() + borderWidth / 2, mTextPaint);
                } else {
                    float baseline = rectF.centerY() + distance;
                    canvas.drawText(drawText, rectF.centerX() - mTextPaint.measureText(drawText) / 2, baseline, mTextPaint);
                }
            }
        }

        mPaint.setColor(borderColor);

        //画竖线
        for (int j = 1; j < rol; j++) {
            canvas.drawRect(j * childerWidth - borderWidth / 2, 0, j * childerWidth + borderWidth / 2, height, mPaint);
        }

        //画横线
        canvas.drawRect(0, height / 2 - borderWidth / 2, width, height / 2 + borderWidth / 2, mPaint);

    }

    private String getTimeForNum(int num) {

        if (num == AM) {
            return "上午";
        } else if (num == PM) {
            return "下午";
        } else {
            return num + ":00";
        }

    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

//        switch (event.getAction()) {
//
//            case MotionEvent.ACTION_DOWN:
//                Log.e(TAG, "dispatchTouchEvent: 按下，，，，，，，，，，，，，，，，，，");
//                float downX = event.getX();
//                float downY = event.getY();
//
//                int downPosition = getCoordinateToPosition(downX, downY / childerHeight < 1);
//
//                //如果按下的区域不是“上午”“下午”
//                if (downPosition >= 0) {
//                    checkToJoinList(downPosition);
//                }
//
//                break;
//            case MotionEvent.ACTION_MOVE:
//                Log.e(TAG, "dispatchTouchEvent: 滑动了，，，，，，，，，，，，，，，，，，");
//                float moveX = event.getX();
//                float moveY = event.getY();
//
//                int nowPosition = getCoordinateToPosition(moveX, moveY / childerHeight < 1);
//                Log.e(TAG, "dispatchTouchEvent: nowPosition" + nowPosition);
//                Log.e(TAG, "dispatchTouchEvent: lastTouchPosition" + lastTouchPosition);
//
//                //如果当前滑动到view之外或上午、下午区域，则拦截
////                if (moveX < 0 || moveY < 0 || moveX > width || moveY > height || nowPosition == lastTouchPosition || nowPosition < 0) {
////                    Log.e(TAG, "dispatchTouchEvent: 不拦截");
////                    return false;
////                }
//                Log.e(TAG, "dispatchTouchEvent: 拦截滑动");
//                if (nowPosition >= 0 && nowPosition != lastTouchPosition) {
//                    checkToJoinList(nowPosition);
//                }
//
//                return true;
//
//            case MotionEvent.ACTION_UP:
//
//                lastTouchPosition = NONE;
//                break;
//        }
//
//        return true;

        boolean b = super.dispatchTouchEvent(event);
        Log.e(TAG, "dispatchTouchEvent: "+b );
        return b;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "onTouchEvent: 按下，，，，，" );
                float downX = event.getX();
                float downY = event.getY();

                int downPosition = getCoordinateToPosition(downX, downY / childerHeight < 1);

                //如果按下的区域不是“上午”“下午”
                if (downPosition >= 0) {
                    checkToJoinList(downPosition);
                }

                break;
            case MotionEvent.ACTION_MOVE:
                Log.e(TAG, "onTouchEvent: 滑动了，，，，，，，，，，，，，，，，，，");
                listener.onScrollStateChange(true);
                float moveX = event.getX();
                float moveY = event.getY();

                int nowPosition = getCoordinateToPosition(moveX, moveY / childerHeight < 1);
                Log.e(TAG, "onTouchEvent: nowPosition" + nowPosition);
                Log.e(TAG, "onTouchEvent: lastTouchPosition" + lastTouchPosition);

                //如果当前滑动到view之外或上午、下午区域，则拦截
                if (moveX < 0 || moveY < 0 || moveX > width || moveY > height ) {
                    Log.e(TAG, "onTouchEvent: 不拦截");
                   // listener.onScrollStateChange(false);
                    return false;
                }
                Log.e(TAG, "onTouchEvent: 拦截滑动");
                if (nowPosition >= 0 && nowPosition != lastTouchPosition) {
                    checkToJoinList(nowPosition);
                }

                break;
            case MotionEvent.ACTION_UP:
                Log.e(TAG, "onTouchEvent: 抬起，，，，");
                lastTouchPosition = NONE;
                listener.onScrollStateChange(false);
                break;
        }

        return true;
    }

    /**
     * 检查是否加入集合
     *
     * @param position 当前时间
     */
    private void checkToJoinList(int position) {
        if (selectedList == null) {
            selectedList = new ArrayList<>();
        }

        //如果当前点击、滑动的区域不是 0:00 -- 23:00 则拦截
        if (position < 0) {
            return;
        }

        Integer nowPosition = new Integer(position);
        //判断是否包含在集合里（是否已经选中）
        boolean listIsContains = selectedList.contains(nowPosition);

        //如果是当前按下/滑动 首次到事件区域
        if (lastTouchPosition < 0) {
            //如果集合包含,则当前滑动是取消选中
            if (listIsContains) {
                //记录当前是取消选中
                isSelect = false;
                selectedList.remove(nowPosition);

            } else {
                //不包含则当前滑动是选中
                isSelect = true;
                selectedList.add(nowPosition);
                Collections.sort(selectedList);
            }

            postInvalidate();
            if (listener != null) {
                listener.onChangeTime(isSelect, selectedList);
            }

        } else {
            //选中,并且集合不包含
            if (isSelect && !listIsContains) {
                selectedList.add(nowPosition);
                Collections.sort(selectedList);
                postInvalidate();

                if (listener != null) {
                    listener.onChangeTime(isSelect, selectedList);
                }

            } else if (!isSelect && listIsContains) {  //取消选中，并且集合包含，则移除
                selectedList.remove(nowPosition);
                postInvalidate();

                if (listener != null) {
                    listener.onChangeTime(isSelect, selectedList);
                }

            }
        }

        lastTouchPosition = position;
    }

    /**
     * 获取坐标转换的position
     *
     * @param x       x坐标
     * @param isFirst 是否第一行
     * @return 返回当前时间的position（0:00->0   11:00 -> 11   23:00 -> 23   上午-> -2  下午 -> -1）
     */
    private int getCoordinateToPosition(float x, boolean isFirst) {
        if (isFirst) {
            Log.e(TAG, "getCoordinateToPosition: 点击了第一排，，");
        } else {
            Log.e(TAG, "getCoordinateToPosition: 点击了第二排，，");
        }
        //判断是否第一列
        if (x / childerWidth < 1) {
            //第一列第一行是上午
            if (isFirst) {
                return AM;
            } else {  //第一列第二行是下午
                return PM;
            }
        } else {

            //计算其他的position位置（0:00->0   11:00 -> 11   23:00 -> 23）
            int tempPosition = (int) (x / childerWidth) - 1;
            return isFirst ? tempPosition : tempPosition + rol - 1;
        }
    }

    /**
     * 设置选中的集合
     *
     * @param selectedList
     */
    public void setSelectedList(List<Integer> selectedList) {
        this.selectedList = selectedList;
        postInvalidate();
    }

    public void setMaxPriceRecord(Map<Integer, Object> maxPriceRecord) {
        if (maxPriceRecord==null){
            return;
        }
        this.maxPriceRecord = maxPriceRecord;
        postInvalidate();
    }

    public List<Integer> getSelectedList() {
        return selectedList;
    }

    private OnChangeTimeListener listener;

    public void setOnChangeTimeListener(OnChangeTimeListener listener) {
        this.listener = listener;
    }

    public interface OnChangeTimeListener {
        void onChangeTime(boolean isSelect, List<Integer> seletedList);

        void  onScrollStateChange(boolean scrolling);
    }

}
